<!--
 * @Description: 属性管理
 * @Author: 龙春雨
 * @Date: 2023-04-11 10:15:19
-->
<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @size-change="sizeChange"
      @current-change="currentChange"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDelete"
    >
      <template v-slot:menu="{row, index}">
        <el-button
          type="text"
          size="small"
          icon="el-icon-menu"
          @click="handleOpenOption(row,index)"
        >属性项
        </el-button>
      </template>
    </avue-crud>
    <option-dialog
      :visible.sync="optionDialogVisible"
      :attribute-id="attributeId"
      :attribute-name="attributeName"
      :attribute-code="attributeCode"
    />
  </basic-container>
</template>

<script>
import { tableOption } from '@/const/crud/admin/classifyAttribute';
import optionDialog from './components/optionDialog.vue';
import {
  getPetAttributePage,
  addPetAttribute,
  editPetAttribute,
  delPetAttribute,
} from '@/api/core/attribute';
import crudMixins from '@/mixins/crud';
export default {
  name: 'Attribute',
  components: {
    optionDialog,
  },
  mixins: [crudMixins],
  data() {
    return {
      tableOption,
      form: {},
      treeData: [],
      treeOption: {
        nodeKey: 'id',
        addBtn: false,
        menu: false,
        props: {
          label: 'name',
          value: 'id',
        },
      },
      optionDialogVisible: false,
      attributeId: '',
      attributeName: '',
      attributeCode: '',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取列表
    async getList() {
      try {
        this.tableLoading = true;
        const { data } = await getPetAttributePage({
          size: this.page.pageSize,
          current: this.page.currentPage,
          ...this.searchForm,
        });
        this.tableData = data.data.records;
        this.page.total = data.data.total;
        this.tableLoading = false;
      } catch (error) {
        this.tableLoading = false;
        throw new Error(error);
      }
    },
    // 编辑
    async handleUpdate(row, index, done, loading) {
      row.parentAttributeCode = 0;
      row.attributeType = 0;
      try {
        await editPetAttribute({
          ...row,
        });
        done();
        this.getList();
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    // 保存
    async handleSave(row, done, loading) {
      row.parentAttributeCode = 0;
      row.attributeType = 0;
      try {
        await addPetAttribute({
          ...row,
        });
        done();
        this.getList();
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    // 删除
    handleDelete: function (row, index) {
      this.$confirm('是否确认删除该宠物属性?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delPetAttribute(row.id);
        })
        .then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
    },
    // 打开前的拦截
    beforeOpen(done) {
      this.form.code = this.searchForm.code;
      this.form.name = this.searchForm.name;

      done();
    },
    nodeClick(data) {
      this.searchForm.code = data.code;
      this.searchForm.name = data.name;
      this.getList();
    },

    // 打开属性项
    handleOpenOption(row, index) {
      this.optionDialogVisible = true;
      this.attributeId = row.id;
      this.attributeName = row.attributeName;
      this.attributeCode = row.attributeCode;
    },
  },
};
</script>

<style lang="scss">
.dialog-classify {
  .el-collapse-item__header {
    margin-bottom: 0;
  }
  .avue-upload {
    position: absolute;
    width: 100%;
  }
}
.classify__tree {
  .el-tree-node__content {
    height: 40px;
  }
}
</style>
